<?php
function loadPage_custom($template, $message) {
  switch ($template) {
    case 'knowledges':
      knowledges($message);
      break;
    case 'knowledge':
      knowledge($message);
      break;
    case 'knowledgebases':
      knowledgebases($template, $message);
      break;
    case 'knowledgebase':
      knowledgebase($template, $message);
      break;
    default:
      header('Location: http://' . DOMAIN . PATH);
      break;
  }
}

function knowledges($message) {
  global $smarty;
  $message['k'] = DbUtils::get(DbUtils::createPdoInst(), 'pz_knowledge', NULL, NULL, NULL, NULL, NULL, NULL)->d;
  $smarty->assign('msg', json_encode($message));
}

function knowledge($message) {
  global $smarty;
  $id = MiscUtils::getParam('i', NULL);
  if ($id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $message[0] = DbUtils::get(DbUtils::createPdoInst(), 'pz_knowledge', $cond_vals, NULL, NULL, NULL, NULL, NULL);
    
    foreach ($message[0]->d as &$knowledge) {
	    $cond_vals = new stdClass();
	    $cond_vals->c = 't.knowledge_trackId = :v1';
	    $cond_vals->v = array(':v1' => $knowledge->track_id);
    }
    $message['img'] = DbUtils::get(DbUtils::createPdoInst(), TABLE_NAME_PREFIX_CUSTOM . 'knowledge' . '_image', $cond_vals, NULL, NULL, NULL, NULL, NULL);
    
    $message['idx'] = $id;
    $smarty->assign('msg', json_encode($message));
  }
}

function knowledgebases($template, $message) {
  global $smarty;
  $page = MiscUtils::getParam('p', 1);
  $size = 6;
  $id = MiscUtils::getParam('i', 1);
  $type = MiscUtils::getParam('type', '');
  
  if ($type) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.type = :v1';
    $cond_vals->v = array(':v1' => $type);
    $message['k'] = DbUtils::get(DbUtils::createPdoInst(), 'pz_knowledge', $cond_vals, NULL, NULL, $page, $size, NULL);
  } else {
    $message['k'] = DbUtils::get(DbUtils::createPdoInst(), 'pz_knowledge', NULL, NULL, NULL, $page, $size, NULL);
  }
  $message['temp'] = $template;
  $message['idx'] = $id;
  $message['type'] = $type;
  $smarty->assign('idx', $id);
  $smarty->assign('msg', json_encode($message));
}

function knowledgebase($template, $message) {
  global $smarty;
  $id = MiscUtils::getParam('i', 1);
  $kid = MiscUtils::getParam('k', 1);
  $type = MiscUtils::getParam('type', '');
  
  $message['type'] = $type;
  $message['temp'] = $template;
  
  if ($kid) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id = :v1';
    $cond_vals->v = array(':v1' => $kid);
    $message[0] = DbUtils::get(DbUtils::createPdoInst(), 'pz_knowledge', $cond_vals, NULL, NULL, NULL, NULL, NULL);
    $content = (count($message[0]->d) == 0) ? '' : $message[0]->d[0]->detail;
    
    $message['idx'] = $id;
  
    $smarty->assign('msg', json_encode($message));
    $smarty->assign('content', $content);
  } else {
    $smarty->assign('content', '');
  }
}
?>